﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FreedBack.Helpers
{
    public static class ExtensionMethods
    {
        /// <summary>
        /// *** EXTENSION METHOD *** Gets an existing list but adds the supplied extraItem to it in the first position
        /// </summary>
        /// <typeparam name="T">The type of list</typeparam>
        /// <param name="data">The actual list to add to</param>
        /// <param name="extraItem">The item to add in the first position</param>
        /// <returns></returns>
        public static IEnumerable<T> GetWithExtraFirstItem<T>(this IEnumerable<T> data, T extraItem)
        {
            var items = new List<T>();
            items.Add(extraItem);
            if (data != null)
            {
                items.AddRange(data);
            }
            return items;
        }

        /// <summary>
        /// *** EXTENSION METHOD *** Gets an existing list but adds the supplied text/value item in the first position
        /// </summary>
        /// <param name="data">The type of list</param>
        /// <param name="text">The text of the item to add</param>
        /// <param name="value">The value of the item to add</param>
        /// <returns></returns>
        public static IEnumerable<SelectListItem> GetWithExtraFirstItem(this IEnumerable<SelectListItem> data, string text, string value)
        {
            var items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = text, Value = value });
            if (data != null)
            {
                items.AddRange(data);
            }
            return items;
        }

        /// <summary>
        /// *** EXTENSION METHOD *** Gets an existing list but adds an empty text item with a value of "0" as the first item
        /// </summary>
        /// <param name="data">The existing list to add the item to</param>
        /// <returns></returns>
        public static IEnumerable<SelectListItem> GetWithEmptyFirstItem(this IEnumerable<SelectListItem> data)
        {
            var items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = "", Value = "0" });
            if (data != null)
            {
                items.AddRange(data);
            }
            return items;
        }
    }
}